package org.dromara.business.service.impl;

import org.dromara.business.domain.CompanyResumeLibrary;
import org.dromara.business.service.CompanyResumeLibraryService;
import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.BusinessInformationBo;
import org.dromara.business.domain.vo.BusinessInformationVo;
import org.dromara.business.domain.BusinessInformation;
import org.dromara.business.mapper.BusinessInformationMapper;
import org.dromara.business.service.BusinessInformationService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 信息Service业务层处理
 *
 * @author zlj
 * @date 2024-11-18
 */
@RequiredArgsConstructor
@Service
public class BusinessInformationServiceImpl extends ServiceImpl<BusinessInformationMapper,BusinessInformation> implements BusinessInformationService {

    private final BusinessInformationMapper baseMapper;
    private final CompanyResumeLibraryService companyResumeLibraryService;

    /**
     * 查询信息
     */
    @Override
    public BusinessInformationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询信息列表
     */
    @Override
    public TableDataInfo<BusinessInformationVo> queryPageList(BusinessInformationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusinessInformation> lqw = buildQueryWrapper(bo);
        Page<BusinessInformationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询信息列表
     */
    @Override
    public List<BusinessInformationVo> queryList(BusinessInformationBo bo) {
        LambdaQueryWrapper<BusinessInformation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusinessInformation> buildQueryWrapper(BusinessInformationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusinessInformation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFlId() != null, BusinessInformation::getFlId, bo.getFlId());
        lqw.eq(StrUtil.isNotBlank(bo.getType()), BusinessInformation::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getBt()), BusinessInformation::getBt, bo.getBt());
        lqw.eq(StrUtil.isNotBlank(bo.getNr()), BusinessInformation::getNr, bo.getNr());
        lqw.eq(StrUtil.isNotBlank(bo.getPhoto()), BusinessInformation::getPhoto, bo.getPhoto());
        lqw.eq(StrUtil.isNotBlank(bo.getDz()), BusinessInformation::getDz, bo.getDz());
        lqw.eq(StrUtil.isNotBlank(bo.getSf()), BusinessInformation::getSf, bo.getSf());
        lqw.eq(StrUtil.isNotBlank(bo.getLxrXm()), BusinessInformation::getLxrXm, bo.getLxrXm());
        lqw.eq(StrUtil.isNotBlank(bo.getLxrDh()), BusinessInformation::getLxrDh, bo.getLxrDh());
        lqw.eq(StrUtil.isNotBlank(bo.getShzt()), BusinessInformation::getShzt, bo.getShzt());
        lqw.eq(StrUtil.isNotBlank(bo.getShly()), BusinessInformation::getShly, bo.getShly());
        lqw.eq(StrUtil.isNotBlank(bo.getLxNameYj()), BusinessInformation::getLxNameYj, bo.getLxNameYj());
        lqw.eq(StrUtil.isNotBlank(bo.getLxNameEj()), BusinessInformation::getLxNameEj, bo.getLxNameEj());
        lqw.eq(bo.getUserId() != null, BusinessInformation::getUserId, bo.getUserId());
        lqw.orderByDesc(BusinessInformation::getCreateTime);
        return lqw;
    }

    /**
     * 新增信息
     */
    @Override
    public Boolean insertByBo(BusinessInformationBo bo) {
        BusinessInformation add = MapstructUtils.convert(bo, BusinessInformation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改信息
     */
    @Override
    public Boolean updateByBo(BusinessInformationBo bo) {
        BusinessInformation update = MapstructUtils.convert(bo, BusinessInformation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BusinessInformation entity){

    }

    /**
     * 批量删除信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        if (null != ids && ids.size() > 0){
            companyResumeLibraryService.lambdaUpdate()
                .in(CompanyResumeLibrary::getBusinessInformationId,ids)
                .set(CompanyResumeLibrary::getCreateTime,new Date()).update();
        }

        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
